﻿angular.module('farfar.controllers', [])
.controller('TestCtrl', function($scope,JFHttp) {
	$scope.login=function(name,pwd){
		JFHttp.post({
			url:"http://192.168.1.101:8080/user/login",
			data:{
				userId:name,
				userPassword:pwd
			}
		}).then(function(data){
			//这是成功的参数
			console.log(data);
			$scope.userid=data.id;
			$scope.userName=data.name;
		},function(data){
			console.log(data);
			/**
			 * data={
			 * 	status:"200",
			 * }
			 */
		});
	}
})
/**
 * 登录控制器 denglu
 */
.controller('LoginCtrl', function($scope,User,$state) {
    $scope.user={};
	$scope.errorLogin=false;
	$scope.loginUser=function(user){
		if(user.id&&user.pwd){
			User.login(user.id,user.pwd).then(
				//登录成功
				function(data){
					$state.go("app.home");
					return true;
				},
				//登录失败
				function(status){
					if(status=="401"){
						$scope.errorLogin=true;
					}
				}
			);
		}
	}
})
/**
 * 注册
 */
.controller('RegistCtrl', function($scope,User) {
	$scope.user={};
  $scope.regist=function(){
	  User.regist($scope.user.name,$scope.user.pwd).then(
		  function(data){
			  console.log(data);
		  },function(data){
			  console.log(data);
		  }
	  );
	 
  }
  ;
})
.controller('HomeCtrl', function($scope,JFHttp) {
  localStorage.clear();
  JFHttp.post({
	  url:"http://192.168.1.101:8080/config/total"
  }).then(function(result){
	  $scope.ele=result.data;
  },function(result){})
 
})
//ObjManaCtrl
.controller('ObjManaCtrl', function($scope,$state,SweetAlert,JFHttp) {
	localStorage.clear();
	$scope.navToEdit=function(item){
		var uid=(new Date()).valueOf();
		localStorage.setItem(uid+"",JSON.stringify(item));
		$state.go("app.objEdi",{id:uid})
	}
	JFHttp.post({
		url:"http://192.168.1.101:8080/dest/all"
	}).then(function(result){
		$scope.itmes=result.data;
	},function(result){
		alert("获取出错，请稍候再试"+result.status);
	});
	$scope.delDest=function(id){
		alert(id);
		SweetAlert.swal({   
			title: "确定删除?",   
			text: "删除后不可恢复!",   
			type: "warning",   
			showCancelButton: true,   
			confirmButtonColor: "#DD6B55",   
			confirmButtonText: "心意已决，删除它!",   
			cancelButtonText: "不，我再看看",   
			closeOnConfirm: false,   
			closeOnCancel: false 
		}, function(isConfirm){  
			if (isConfirm) {     
				JFHttp.post({
					url:"http://192.168.1.101:8080/dest/del",
					data:{
						id:id
					}
				}).then(function(result){
					SweetAlert.swal("已删除!", "已经删除它了.", "success"); 
				},function(result){
					SweetAlert.swal("删除失败", "", "error");   
				})
				  
			} else {     
				SweetAlert.swal("已取消", "已经取消", "error");   
			} 
		});
	}  
})
//ObjAddCtrl 目的地添加
.controller('ObjAddCtrl', ['$scope', '$http','$state','JFHttp', function($scope, $http,$state,JFHttp) {
    $scope.reader = new FileReader();   //创建一个FileReader接口
    $scope.thumb = {};      //用于存放图片的base64
    $scope.thumb_default = {    //用于循环默认的‘加号’添加图片的框
        1111:{}
    };
	$scope.belongTo="";
    $scope.img_upload = function(files) {       //单次提交图片的函数
        $scope.guid = (new Date()).valueOf();   //通过时间戳创建一个随机数，作为键名使用
        $scope.reader.readAsDataURL(files[0]);  //FileReader的方法，把图片转成base64
        $scope.reader.onload = function(ev) {
            $scope.$apply(function(){
                $scope.thumb[$scope.guid] = {
                    imgSrc : ev.target.result,  //接收base64
                }
            });
        };
        
        var data = new FormData();      //以下为像后台提交图片数据
        data.append('file', files[0]);
        data.append('name',"1213");  
		data.append('belongTo',$scope.belongTo);               
        $http({
            method: 'post',
            url: 'http://192.168.1.101:8080/file/saveImg',
            data:data,
              transformRequest: angular.identity ,
            headers: {'optional-header': 'header-value','Content-Type':undefined},
        }).success(function(data) {
			 $scope.thumb[$scope.guid].id=data.data.id;
			 $scope.belongTo=data.data.belongTo;
             console.log( $scope.thumb[$scope.guid]);
                    
        })
    };

    $scope.img_del = function(key) {    //删除，删除的时候thumb和form里面的图片数据都要删除，避免提交不必要的
        var guidArr = [];
        for(var p in $scope.thumb){
            guidArr.push(p);
        }
		var obbj= $scope.thumb[guidArr[key]];
		JFHttp.post({
			url:"http://192.168.1.101:8080/file/delImg",
			data:{
				id:obbj.id
			}
		}).then(function(data){
			delete $scope.thumb[guidArr[key]];
		},function(data){
			alert("删除出错");
		});
    };
	$scope.addADest=function(obj){
		obj.affixBelongTo=$scope.belongTo;
		JFHttp.post({
			url:"http://192.168.1.101:8080/dest/add",
			data:obj,
		}).then(function(result){
			alert("添加成功");
			$state.go("app.objMana");
		},function(result){
			alert("添加出错，请稍候再试。"+result.status);
		});
	}
}])

//目的地编辑objEdiCtrl
.controller('ObjEdiCtrl',['$scope','$http','$state','$stateParams','JFHttp',function($scope,$http,$state,$stateParams,JFHttp){
	var idd=$stateParams.id;
	var tmps=localStorage.getItem(idd);
	if(!tmps)return;
	var item=JSON.parse(tmps);
	if(!item)alert("访问出错");
	item.index=parseInt(item.index);
	 $scope.dest=item;
	 localStorage.removeItem(idd);
	console.log(item);
	$scope.belongTo=item.affixBelongTo;
	$scope.reader = new FileReader();   //创建一个FileReader接口
    $scope.thumb = {};      //用于存放图片
	var tmppic={};
	if($scope.dest.pics!=null&&$scope.dest.pics.length>0){
		var picsd=	$scope.dest.pics;
		var tmpi=0;
		for(tmpi=0;tmpi<picsd.length;tmpi++){
			$scope.guid = (new Date()).valueOf()+Math.random()*100;
			tmppic[$scope.guid] = {
                    imgSrc : picsd[tmpi].imgSrc,  
					id:picsd[tmpi].id,
                };
		}
		// $scope.dest.pics.forEach(function(element) {
		// 	$scope.guid = (new Date()).valueOf();
		// 	tmppic[$scope.guid] = {
        //             imgSrc : element.imgSrc,  
		// 			id:element.id,
        //         }
		// 		console.log(tmppic);
		// }, this);
		
		console.log(tmppic);
		 $scope.thumb=tmppic;
	}
    $scope.img_upload = function(files) {       //单次提交图片的函数
     //通过时间戳创建一个随机数，作为键名使用
	   $scope.guid = (new Date()).valueOf();
        $scope.reader.readAsDataURL(files[0]);  //FileReader的方法，把图片转成base64
        $scope.reader.onload = function(ev) {
            $scope.$apply(function(){
                $scope.thumb[$scope.guid] = {
                    imgSrc : ev.target.result,  //接收base64
                }
            });
        };
        
        var data = new FormData();      //以下为像后台提交图片数据
        data.append('file', files[0]);
        data.append('name',"1213");  
		data.append('belongTo',$scope.belongTo);               
        $http({
            method: 'post',
            url: 'http://192.168.1.101:8080/file/saveImg',
            data:data,
              transformRequest: angular.identity ,
            headers: {'optional-header': 'header-value','Content-Type':undefined},
        }).success(function(data) {
			 $scope.thumb[$scope.guid].id=data.data.id;
			  $scope.thumb[$scope.guid].belongTo=data.data.belongTo;
			 $scope.belongTo=data.data.belongTo;
             console.log( $scope.thumb[$scope.guid]);
                    
        })
    };
    $scope.img_del = function(key) {    //删除，删除的时候thumb和form里面的图片数据都要删除，避免提交不必要的
        var guidArr = [];
        for(var p in $scope.thumb){
            guidArr.push(p);
        }
		var obbj= $scope.thumb[guidArr[key]];
		JFHttp.post({
			url:"http://192.168.1.101:8080/file/delImg",
			data:{
				id:obbj.id
			}
		}).then(function(data){
			delete $scope.thumb[guidArr[key]];
		},function(data){
			alert("删除出错");
		})
    };
	$scope.editObj=function(obj){
		obj.affixBelongTo=$scope.belongTo;
		obj.indexFor=obj.index;
		delete obj.pics;
		delete obj.$$hashKey;
		delete obj.index;
		JFHttp.post({
			url:"http://192.168.1.101:8080/dest/edit",
			data:obj,
		}).then(function(result){
			alert("编辑成功");
			$state.go("app.objMana");
		},function(result){
			alert("修改出错，请稍候再试。"+result.status);
		});
	};
}])

//broadcastCtrl
.controller('broadcastCtrl', function($scope,$state,JFHttp) {
	$scope.content="";
	$scope.send=function(item){
		JFHttp.post({
			url:"http://192.168.1.101:8080/msg/broadcast",
			data:{
				content:item,
			}
		}).then(function(result){
			alert("发送成功");
		},function(result){

		})
	} 
})


//wayAddCtrl
.controller('wayAddCtrl', ['$scope', '$http','$state','JFHttp', function($scope, $http,$state,JFHttp) {
	$scope.destarrs=[];
	$scope.way={};
	$scope.tripBelongTo;
	$scope.userArray=[];
	JFHttp.post({
		url:"http://192.168.1.101:8080/dest/maindest",
	}).then(function(result){
		$scope.destarrs=result.data;
	},function(result){
		alert("获取出错");
	});
	JFHttp.post({
		url:"http://192.168.1.101:8080/user/getmain"
	}).then(function(result){
		$scope.userArray=result.data;
	},function(result){
		alert("拉取用户失败");
	});
    $scope.reader = new FileReader();   //创建一个FileReader接口
    $scope.thumb = {};      //用于存放图片的base64
	$scope.belongTo="";
	/**
	 * 上传线路图片
	 */
	$scope.wayimg_upload= function(files) {       //单次提交图片的函数
        $scope.guid = (new Date()).valueOf();   //通过时间戳创建一个随机数，作为键名使用
        $scope.reader.readAsDataURL(files[0]);  //FileReader的方法，把图片转成base64
        $scope.reader.onload = function(ev) {
            $scope.$apply(function(){
                $scope.way.imgSrc= ev.target.result; //接收base64
               
            });
        };
        
        var data = new FormData();      //以下为像后台提交图片数据
        data.append('file', files[0]);
        data.append('name',"1213");
        $http({
            method: 'post',
            url: 'http://192.168.1.101:8080/file/saveImg',
            data:data,
              transformRequest: angular.identity ,
            headers: {'optional-header': 'header-value','Content-Type':undefined},
        }).success(function(data) {
			 $scope.belongTo=data.data.belongTo;
        })
    };
    $scope.img_upload = function(files) {       //单次提交图片的函数
        $scope.guid = (new Date()).valueOf();   //通过时间戳创建一个随机数，作为键名使用
        $scope.reader.readAsDataURL(files[0]);  //FileReader的方法，把图片转成base64
        $scope.reader.onload = function(ev) {
            $scope.$apply(function(){
                $scope.thumb[$scope.guid] = {
                    imgSrc : ev.target.result,  //接收base64
                };
            });
        };
        
        var data = new FormData();      //以下为像后台提交图片数据
        data.append('file', files[0]);
        data.append('name',"1213");
        $http({
            method: 'post',
            url: 'http://192.168.1.101:8080/file/saveImg',
            data:data,
              transformRequest: angular.identity ,
            headers: {'optional-header': 'header-value','Content-Type':undefined},
        }).success(function(data) {
			 $scope.thumb[$scope.guid].id=data.data.id;
			  $scope.thumb[$scope.guid].belongTo=data.data.belongTo;
			 //$scope.belongTo=data.data.belongTo;
             console.log( $scope.thumb[$scope.guid]);
        })
    };
	/**
	 * 添加行程的文字描述，并获得tripBelongTo
	 */
	$scope.img_textAdd=function(key){
		var guidArr = [];
        for(var p in $scope.thumb){
            guidArr.push(p);
        }
		var obbj= $scope.thumb[guidArr[key]];
		JFHttp.post({
			url:"http://192.168.1.101:8080/trip/add",
			data:{
				tripBelongTo:$scope.tripBelongTo,
				affixBelongTo:obbj.belongTo,
				description:obbj.description,
			}
		}).then(function(result){
			var tripInfo=result.data;
			$scope.thumb[guidArr[key]].tripId=tripInfo.id;
			$scope.tripBelongTo=tripInfo.tripBelongTo;
		},function(result){
			alert("添加失败");
		})
	}
    $scope.img_del = function(key) {    //删除，删除的时候thumb和form里面的图片数据都要删除，避免提交不必要的
        var guidArr = [];
        for(var p in $scope.thumb){
            guidArr.push(p);
        }
		var obbj= $scope.thumb[guidArr[key]];
		JFHttp.post({
			url:"http://192.168.1.101:8080/trip/del",
			data:{
				id:obbj.tripId,
				affixId:obbj.id,
			}
		}).then(function(data){
			delete $scope.thumb[guidArr[key]];
		},function(data){
			alert("删除出错");
		})
    $scope.addWay=function(way){
		obj.affixBelongTo=$scope.belongTo;
		
	}
        //delete $scope.form.image[guidArr[key]];
    };
	$scope.addRoute=function(item){
		item.tripBelongTo=$scope.tripBelongTo;
		item.tripBelongTo=$scope.tripBelongTo;
		$scope.userArray.forEach(function(element) {
			if(element.id==item.leaderId){
				item.leaderName=element.nickName;
			}
		}, this);
		item.affixBelongTo=$scope.belongTo;
		delete item.imgSrc;
		JFHttp.post({
			url:"http://192.168.1.101:8080/route/add",
			data:item,
		}).then(function(result){
			alert("添加成功");
			$state.go("app.wayMana");
		},function(result){
			alert("添加出错，请稍候再试。"+result.status);
		});
	}
}])
/**
 * 路线查看
 */
.controller("waySeeCtrl",function($scope,$state,$stateParams,JFHttp){
	var uid=$stateParams.id;
	var tstrjson=localStorage.getItem(uid);
	if(!tstrjson)return;
	$scope.way=JSON.parse(tstrjson);
	console.log($scope.way);
	$scope.thumb = $scope.way.triparray;
	JFHttp.post({
		url:"http://192.168.1.101:8080/dest/maindest",
	}).then(function(result){
		$scope.destarrs=result.data;
	},function(result){
		alert("获取出错");
	})
	;
	$scope.userArray=[];
	JFHttp.post({
		url:"http://192.168.1.101:8080/user/getmain"
	}).then(function(result){
		$scope.userArray=result.data;
	},function(result){
		alert("拉取用户失败");
	});
})
/**
 * 线路编辑
 */
.controller('wayEdiCtrl', ['$scope', '$http','$state','$stateParams','JFHttp', function($scope, $http,$state,$stateParams,JFHttp) {
	var uid=$stateParams.id;
	var tstrjson=localStorage.getItem(uid);
	if(!tstrjson)return;
	$scope.way=JSON.parse(tstrjson);
	console.log($scope.way);
	$scope.destarrs=[];
	$scope.tripBelongTo=$scope.way.tripBelongTo;
	$scope.way.destinationId=parseInt($scope.way.destinationId);
	JFHttp.post({
		url:"http://192.168.1.101:8080/dest/maindest",
	}).then(function(result){
		$scope.destarrs=result.data;
	},function(result){
		alert("获取出错");
	});
	$scope.userArray=[];
	JFHttp.post({
		url:"http://192.168.1.101:8080/user/getmain"
	}).then(function(result){
		$scope.userArray=result.data;
	},function(result){
		alert("拉取用户失败");
	});
    $scope.reader = new FileReader();   //创建一个FileReader接口
    $scope.thumb = $scope.way.triparray;      //用于存放图片的base64
	$scope.belongTo=$scope.affixBelongTo;
	/**
	 * 上传线路图片
	 */
	$scope.wayimg_upload= function(files) {       //单次提交图片的函数
        $scope.guid = (new Date()).valueOf();   //通过时间戳创建一个随机数，作为键名使用
        $scope.reader.readAsDataURL(files[0]);  //FileReader的方法，把图片转成base64
        $scope.reader.onload = function(ev) {
            $scope.$apply(function(){
                $scope.way.imgSrc= ev.target.result; //接收base64
               
            });
        };
        
        var data = new FormData();      //以下为像后台提交图片数据
        data.append('file', files[0]);
        data.append('name',"1213");
        $http({
            method: 'post',
            url: 'http://192.168.1.101:8080/file/saveImg',
            data:data,
              transformRequest: angular.identity ,
            headers: {'optional-header': 'header-value','Content-Type':undefined},
        }).success(function(data) {
			 $scope.belongTo=data.data.belongTo;
        })
    };
    $scope.img_upload = function(files) {       //单次提交图片的函数
        $scope.guid = (new Date()).valueOf();   //通过时间戳创建一个随机数，作为键名使用
        $scope.reader.readAsDataURL(files[0]);  //FileReader的方法，把图片转成base64
        $scope.reader.onload = function(ev) {
            $scope.$apply(function(){
                $scope.thumb[$scope.guid] = {
                    imgSrc : ev.target.result,  //接收base64
                };
            });
        };
        
        var data = new FormData();      //以下为像后台提交图片数据
        data.append('file', files[0]);
        data.append('name',"1213");
        $http({
            method: 'post',
            url: 'http://192.168.1.101:8080/file/saveImg',
            data:data,
              transformRequest: angular.identity ,
            headers: {'optional-header': 'header-value','Content-Type':undefined},
        }).success(function(data) {
			 $scope.thumb[$scope.guid].id=data.data.id;
			  $scope.thumb[$scope.guid].belongTo=data.data.belongTo;
			 //$scope.belongTo=data.data.belongTo;
             console.log( $scope.thumb[$scope.guid]);
        })
    };
	/**
	 * 添加行程的文字描述，并获得tripBelongTo
	 */
	$scope.img_textAdd=function(key){
		var guidArr = [];
        for(var p in $scope.thumb){
            guidArr.push(p);
        }
		var obbj= $scope.thumb[guidArr[key]];
		JFHttp.post({
			url:"http://192.168.1.101:8080/trip/add",
			data:{
				tripBelongTo:$scope.tripBelongTo,
				affixBelongTo:obbj.affixBelongTo,
				description:obbj.description,
				id:obbj.tripRouteId,
			}
		}).then(function(result){
			var tripInfo=result.data;
			$scope.thumb[guidArr[key]].tripId=tripInfo.id;
			$scope.tripBelongTo=tripInfo.tripBelongTo;
		},function(result){
			alert("添加失败");
		})
	}
    $scope.img_del = function(key) {    //删除，删除的时候thumb和form里面的图片数据都要删除，避免提交不必要的
        var guidArr = [];
        for(var p in $scope.thumb){
            guidArr.push(p);
        }
		var obbj= $scope.thumb[guidArr[key]];
		JFHttp.post({
			url:"http://192.168.1.101:8080/trip/del",
			data:{
				id:obbj.tripId,
				affixId:obbj.id,
			}
		}).then(function(data){
			delete $scope.thumb[guidArr[key]];
		},function(data){
			alert("删除出错");
		})
    $scope.addWay=function(way){
		obj.affixBelongTo=$scope.belongTo;
		
	}
        //delete $scope.form.image[guidArr[key]];
    };
	$scope.addRoute=function(item){
		item.tripBelongTo=$scope.tripBelongTo;
		$scope.userArray.forEach(function(element) {
			if(element.id==item.leaderId){
				item.leaderName=element.nickName;
			}
		}, this);
		item.belongTo=$scope.belongTo;
		delete item.imgSrc;
		JFHttp.post({
			url:"http://192.168.1.101:8080/route/edit",
			data:item,
		}).then(function(result){
			alert("修改成功");
			$state.go("app.wayMana");
		},function(result){
			alert("修改出错，请稍候再试。"+result.status);
		});
	}
}])
//wayManaCtrl
/**
 * 线路管理
 */
.controller('wayManaCtrl', function($scope,$state,SweetAlert,JFHttp) {
	localStorage.clear();
  $scope.itmes=[];
  JFHttp.post({
	  url:"http://192.168.1.101:8080/route/getall",
  }).then(function(result){
	  $scope.itmes=result.data;
  },function(result){
	  alert("加载线路失败，请稍候再试");
  });
  $scope.editWay=function(item){
	  if(!item)return;
	  var uid= (new Date()).valueOf();
	  localStorage.setItem(uid,JSON.stringify(item));
	  $state.go("app.wayEdi",{
		  id:uid,
	  });
  };
  $scope.lookWay=function(item){
	  if(!item)return;
	  var uid= (new Date()).valueOf();
	  localStorage.setItem(uid,JSON.stringify(item));
	  $state.go("app.waySee",{
		  id:uid,
	  });
  }
	$scope.delWay=function(id){
		SweetAlert.swal({   
			title: "确定删除?",   
			text: "删除后竟不会再回复!",   
			type: "warning",   
			showCancelButton: true,   
			confirmButtonColor: "#DD6B55",   
			confirmButtonText: "心意已决，删除它!",   
			cancelButtonText: "不，我再看看",   
			closeOnConfirm: false,   
			closeOnCancel: false 
		}, function(isConfirm){  
			if (isConfirm) {     
				JFHttp.post({
					url:"http://192.168.1.101:8080/route/del",
					data:{
						id:id,
					}
				}).then(function(result){
					SweetAlert.swal("已删除!", "已经删除它了.", "success"); 
					$state.go("app.wayMana");
				},function(result){
					SweetAlert.swal("删除失败", "", "error");   
				})
			} else {     
				SweetAlert.swal("已取消", "已经取消掉了", "error");   
			} 
		});
	}  
})

//ordManaCtrl
.controller('ordManaCtrl', function($scope,$state,JFHttp) {
	$scope.items=[];
	JFHttp.post({
		url:"http://192.168.1.101:8080/order/getall",
	}).then(function(result){
		$scope.items=result.data;
	},function(result){

	})
	$scope.navToSee=function(item){
		var uid=(new Date()).valueOf();
		localStorage.setItem(uid+"",JSON.stringify(item));
		$state.go("app.ordSee",{id:uid})
	} 
})
//ordSeeCtrl
.controller('ordSeeCtrl', ['$scope','$stateParams' ,'$state','JFHttp', function($scope,$stateParams,$state,JFHttp) {
	var idd=$stateParams.id;
	var iaa=localStorage.getItem(idd);
	if(!iaa)return;
	var item=JSON.parse(iaa);
	$scope.flag=true;
	if(item.state==0){
		$scope.flag=false;
	};
	$scope.order=item;
	$scope.agree=function(item){
		JFHttp.post({
			url:"http://192.168.1.101:8080/order/edit",
			data:{
				id:item.orderId,
				state:"1",
			}
		}).then(function(result){
			alert("预准成功！");
			$state.go("app.ordMana");
		},function(result){
			alert("预准失败，请稍候再试。。。");
		});
	}
}])

/**
 * 消息管理
 */
.controller('msgManaCtrl', function($scope,$state,JFHttp,SweetAlert) {
  $scope.items=[];
  JFHttp.post({
	  url:"http://192.168.1.101:8080/group/getall",
  }).then(function(result){
	  $scope.items=result.data;
  },function(result){
	  //alert("拉取用户失败，请稍候再试");
  });
  $scope.seeUser=function(item){
	  var gid=(new Date()).valueOf();
	  if(!item)return;
	  localStorage.setItem(gid,JSON.stringify(item));
	  $state.go("app.msgSee",{
		  id:gid,
	  })
  };
})

/**
 * 消息查看
 */
.controller('msgSeeCtrl',['$scope','$stateParams' ,'$state','JFHttp', function($scope,$stateParams,$state,JFHttp) {
	var idd=$stateParams.id;
	$scope.sessionId=idd;
	$scope.agree=function(item){
		JFHttp.post({
			url:"http://192.168.1.101:8080/msg/get",
			data:{
				id:idd,
			}
		}).then(function(result){
			alert("查看成功！");
			//$state.go("app.ordMana");
		},function(result){
			alert("查看失败，请稍候再试。。。");
		});
	}
}])



/**
 * 评论管理
 */
.controller('assManaCtrl',  function($scope,$state,$timeout,SweetAlert,JFHttp) {
	$scope.itmes=[];
	$scope.navToEdit=function(item){
		var uid=(new Date()).valueOf();
		localStorage.setItem(uid+"",JSON.stringify(item));
		$state.go("app.objEdi",{id:uid})
	}
	JFHttp.post({
		url:"http://192.168.1.101:8080/comment/getall"
	}).then(function(result){
		$scope.itmes=result.data;
	},function(result){
		alert("获取出错，请稍候再试"+result.status);
	});
	$scope.upLoad=function(id){
		SweetAlert.swal({   
			title: "确定通过审批?",   
			text: "",   
			type: "warning",   
			showCancelButton: true,   
			confirmButtonColor: "#DD6B55",   
			confirmButtonText: "确定",   
			cancelButtonText: "不，我再看看",   
			closeOnConfirm: false,   
			closeOnCancel: false 
		}, function(isConfirm){  
			if (isConfirm) {     
				JFHttp.post({
					url:"http://192.168.1.101:8080/comment/editstatus",
					data:{
						id:id,
					}
				}).then(function(result){
					if(result.status=="200"){
						SweetAlert.swal("确定", "已经通过", "error");
					}
				},function(result){
					SweetAlert.swal("失败", "通过失败", "error");
				});  
			} else {     
				SweetAlert.swal("已取消", "已经取消掉了", "error");   
			} 
		});
		
	}  
	$scope.delWay=function(id){
		SweetAlert.swal({   
			title: "确定删除"+id+"?",   
			text: "删除后竟不会再回复!",   
			type: "warning",   
			showCancelButton: true,   
			confirmButtonColor: "#DD6B55",   
			confirmButtonText: "心意已决，删除它!",   
			cancelButtonText: "不，我再看看",   
			closeOnConfirm: false,   
			closeOnCancel: false 
		}, function(isConfirm){  
			if (isConfirm) {     
				SweetAlert.swal("已删除!", "已经删除它了.", "success");   
			} else {     
				SweetAlert.swal("已取消", "已经取消掉了", "error");   
			} 
		});
	}  
	
})
//
//userManaCtrl
.controller('userManaCtrl', function($scope,$state,JFHttp,SweetAlert) {
  $scope.itmes=[];
  JFHttp.post({
	  url:"http://192.168.1.101:8080/user/getall",
  }).then(function(result){
	  $scope.items=result.data;
  },function(result){
	  alert("拉取用户失败，请稍候再试");
  });
  $scope.seeUser=function(item){
	  var gid=(new Date()).valueOf();
	  if(!item)return;
	  localStorage.setItem(gid,JSON.stringify(item));
	  $state.go("app.userSee",{
		  id:gid,
	  })
  };
  $scope.delUser=function(id){
		SweetAlert.swal({   
			title: "确定删除?",   
			text: "删除后该用户将不能使用客户端!",   
			type: "warning",   
			showCancelButton: true,   
			confirmButtonColor: "#DD6B55",   
			confirmButtonText: "心意已决，删除它!",   
			cancelButtonText: "不，我再看看",   
			closeOnConfirm: false,   
			closeOnCancel: false 
		}, function(isConfirm){  
			if (isConfirm) {     
				SweetAlert.swal("已删除!", "已经设定它了.", "success");   
			} else {     
				SweetAlert.swal("已取消", "已经取消掉了", "error");   
			} 
		});
	}  
	
})
//userSeeCtrl
.controller('userSeeCtrl',['$scope','$stateParams' ,'$state','JFHttp', function($scope,$stateParams,$state,JFHttp) {
	var idd=$stateParams.id;
	var iaa=localStorage.getItem(idd);
	if(!iaa)return;
	var item=JSON.parse(iaa);
	
	$scope.user=item;
}])

//
.controller('TestCtrl',function($scope,SweetAlert){
    $scope.alert1=function(){
        SweetAlert.swal("第一个提示");
    };
    $scope.alert2=function(){
        SweetAlert.swal("我是标题", "我是内容");
    }
    $scope.alert3=function(){
        SweetAlert.swal("验证成功", "你已经输入正确的验证码", "success")
    }
    $scope.alert4=function(){
        SweetAlert.swal({   
			title: "确定删除?",   
			text: "删除后竟不会再回复!",   
			type: "warning",   
			showCancelButton: true,   
			confirmButtonColor: "#DD6B55",   
			confirmButtonText: "心意已决，删除它!",   
			cancelButtonText: "不，我再看看",   
			closeOnConfirm: false,   
			closeOnCancel: false 
		}, function(isConfirm){  
			if (isConfirm) {     
				SweetAlert.swal("已删除!", "已经删除它了.", "success");   
			} else {     
				SweetAlert.swal("已取消", "已经取消掉了", "error");   
			} 
		});
    }
})

//groupManaCtrl
.controller('groupManaCtrl', function($scope,$state,SweetAlert,JFHttp) {
	$scope.itmes=[];
	$scope.navToEdit=function(item){
		var uid=(new Date()).valueOf();
		localStorage.setItem(uid+"",JSON.stringify(item));
		$state.go("app.objEdi",{id:uid})
	}
	JFHttp.post({
		url:"http://192.168.1.101:8080/group/getall"
	}).then(function(result){
		$scope.itmes=result.data;
		console.log(result.data);
	},function(result){
		alert("获取出错，请稍候再试"+result.status);
	});
	$scope.editGroup=function(item){
		if(!item)return;
		var uid=(new Date()).valueOf();
		localStorage.setItem(uid,JSON.stringify(item));
		$state.go("app.groupEdi",{
			id:uid,
		});
	};
	$scope.seeGroup=function(item){
		if(!item)return;
		var uid=(new Date()).valueOf();
		localStorage.setItem(uid,JSON.stringify(item));
		$state.go("app.groupSee",{
			id:uid,
		});
	};
})

.controller("groupAddCtrl",function($scope,$state,JFHttp){
	$scope.users=[];
	$scope.routes=[];
	$scope.userAraay=[];
	$scope.testUsers={}; //下拉框
	$scope.constUsers={};//用户常量
	$scope.tmpUsers={};//用户选中数组
	$scope.groupBelongTo;
	JFHttp.post({
		url:"http://192.168.1.101:8080/user/getmain"
	}).then(function(result){
		$scope.users=result.data;
		$scope.userAraay=angular.copy($scope.users);
		var tusers={};
		var numi=0;
		$scope.userAraay.forEach(function(element) {
			element.numOfIndex=numi;
			tusers[numi+""]=element;
			numi++;
		}, this);
		$scope.testUsers=tusers;
		$scope.constUsers=angular.copy(tusers);
		console.log($scope.testUsers);
	},function(result){
		alert("加载用户失败");
	});
	JFHttp.post({
		url:"http://192.168.1.101:8080/route/mainroute"
	}).then(function(result){
		$scope.routes=result.data;
	},function(result){
		alert("加载路线失败");
	});
	$scope.addGroup=function(item){
		item.tripTime=item.tripTimeT.format("yyyy-MM-dd");
		if(!item.tripNum)item.tripNum=7;
		console.log(item);
		var objlen=(Object.keys($scope.tmpUsers)).length;
		if(objlen>0){
			item.groupBelongTo=$scope.groupBelongTo;
		}
		JFHttp.post({
			url:"http://192.168.1.101:8080/group/add",
			data:item,
		}).then(function(result){
			alert("增加分组成功");
			$state.go("app.groupMana");
		},function(result){

		});
	};
	$scope.addToGroup=function(item){
		var user=angular.copy(item);
		JFHttp.post({
			url:"http://192.168.1.101:8080/userR2group/add",
			data:{
				groupBelongTo:$scope.groupBelongTo,
				userId:item.id,				
			}
		}).then(function(result){
			$scope.groupBelongTo=result.data.groupBelongTo;
			delete $scope.testUsers[item.numOfIndex];
			var ijj=(Object.keys($scope.tmpUsers)).length;
			user.id=result.data.id;
			//转换用户编号
			user.userId=item.id;
			$scope.tmpUsers[ijj+""]=user;
			$scope.tmpUsers[ijj+""].orderArr=[];
			JFHttp.post({
				url:"http://192.168.1.101:8080/order/getlessbyuser",
				data:{
					userId:item.id,				
				}
			}).then(function(result){
				$scope.tmpUsers[ijj+""].orderArr=result.data;
			},function(result){
				console.error(result);
			});
		},function(result){
		});
	};
	 $scope.img_del = function(key) {    //删除，删除的时候thumb和form里面的图片数据都要删除，避免提交不必要的
        var guidArr = [];
        for(var p in $scope.tmpUsers){
            guidArr.push(p);
        }
		var obbj= $scope.tmpUsers[guidArr[key]];
		console.log(obbj);
		JFHttp.post({
			url:"http://192.168.1.101:8080/userR2group/del",
			data:{
				id:obbj.id
			}
		}).then(function(data){
			var rrjsuser=angular.copy(obbj);
			$scope.testUsers[rrjsuser.numOfIndex+""]=rrjsuser;
			console.log($scope.testUsers);
			delete $scope.tmpUsers[guidArr[key]];
		},function(data){
			alert("删除出错");
		});
    };
	$scope.userOrderChange=function(orderId,user2GroupId){
		JFHttp.post({
			url:"http://192.168.1.101:8080/userR2group/set",
			data:{
				orderId:orderId,
				user2GroupId:user2GroupId
			}
		}).then(function(result){

		},function(result){
			alert("绑定订单出错"+result.status);
		});
	}
})
.controller("groupSeeCtrl",function($scope,$state,$stateParams,JFHttp){
	var idd=$stateParams.id;
	var iaa=localStorage.getItem(idd);
	if(!iaa)return;
	var item=JSON.parse(iaa);
	item.tripTime=new Date(item.tripTime);
	item.tripNum=parseInt(item.tripNum);
	$scope.group=item;
	$scope.group.leaderId=parseInt($scope.group.leaderId);
	$scope.tmpUsers=item.users;//用户选中数组
	JFHttp.post({
		url:"http://192.168.1.101:8080/user/getmain"
	}).then(function(result){
		$scope.users=result.data;
		$scope.userAraay=angular.copy($scope.users);
	},function(result){
		alert("加载用户失败");
	});
})
.controller("groupEditCtrl",function($scope,$state,$stateParams,JFHttp){
	var idd=$stateParams.id;
	var iaa=localStorage.getItem(idd);
	if(!iaa)return;
	var item=JSON.parse(iaa);
	item.tripTimeT=new Date(item.tripTime);
	item.tripNum=parseInt(item.tripNum);
	$scope.group=item;
	console.log(item);
	$scope.group.leaderId=parseInt($scope.group.leaderId);
	var djir={};
	var numi=0;
	item.users.forEach(function(element) {
		//element.numOfIndex=numi;
			djir[element.userId]=element;
			JFHttp.post({
				url:"http://192.168.1.101:8080/order/getlessbyuser",
				data:{
					userId:element.userId,
				}
			}).then(function(result){
					$scope.tmpUsers[element.userId].orderArr=result.data;
			},function(result){
				alert("加载用户订单数据出错");
			});
			//numi++;
	}, this);
	$scope.tmpUsers=djir;//用户选中数组
	$scope.users=[];
	$scope.routes=[];
	$scope.userAraay=[];
	$scope.testUsers={}; //下拉框
	$scope.constUsers={};//用户常量
	$scope.groupBelongTo=$scope.group.groupBelongTo;
	JFHttp.post({
		url:"http://192.168.1.101:8080/user/getmain"
	}).then(function(result){
		$scope.users=result.data;
		$scope.userAraay=angular.copy($scope.users);
		var tusers={};
		var ttusers={};
		$scope.userAraay.forEach(function(element) {
			//element.numOfIndex=numi;
			tusers[element.id+""]=element;
			if(!$scope.tmpUsers[element.id]){
				ttusers[element.id]=element;
			}
		}, this);
		$scope.testUsers=ttusers;
		$scope.constUsers=angular.copy(tusers);
		console.log($scope.testUsers);
	},function(result){
		alert("加载用户失败");
	});
	JFHttp.post({
		url:"http://192.168.1.101:8080/route/mainroute"
	}).then(function(result){
		$scope.routes=result.data;
	},function(result){
		alert("加载路线失败");
	});
	$scope.addGroup=function(item){
		item.tripTime=item.tripTimeT.format("yyyy-MM-dd");
		if(!item.tripNum)item.tripNum=7;
		console.log(item);
		var objlen=(Object.keys($scope.tmpUsers)).length;
		if(objlen>0){
			item.groupBelongTo=$scope.groupBelongTo;
		}
		JFHttp.post({
			url:"http://192.168.1.101:8080/group/edit",
			data:item,
		}).then(function(result){
			alert("修改分组成功");
			$state.go("app.groupMana");
		},function(result){

		});
	};
	$scope.addToGroup=function(item){
		var user=angular.copy(item);
		JFHttp.post({
			url:"http://192.168.1.101:8080/userR2group/add",
			data:{
				groupBelongTo:$scope.groupBelongTo,
				userId:item.id,				
			}
		}).then(function(result){
			$scope.groupBelongTo=result.data.groupBelongTo;
			delete $scope.testUsers[item.id];
			user.userId=user.id;
			user.id=result.data.id;
			user.orderArr=[];
			$scope.tmpUsers[user.userId+""]=user;
			JFHttp.post({
				url:"http://192.168.1.101:8080/order/getlessbyuser",
				data:{
					userId:item.id,				
				}
			}).then(function(result){
				$scope.tmpUsers[item.id+""].orderArr=result.data;
			},function(result){
				console.error(result);
			});
			
		},function(result){

		});
	};
	 $scope.img_del = function(key) {    //删除，删除的时候thumb和form里面的图片数据都要删除，避免提交不必要的
        var guidArr = [];
        for(var p in $scope.tmpUsers){
            guidArr.push(p);
        }
		var obbj= $scope.tmpUsers[guidArr[key]];
		console.log(obbj);
		JFHttp.post({
			url:"http://192.168.1.101:8080/userR2group/del",
			data:{
				id:obbj.id
			}
		}).then(function(data){
			var rrjsuser=angular.copy(obbj);
			if(!rrjsuser.userId){
				$scope.testUsers[rrjsuser.id+""]=rrjsuser;
			}else{
				$scope.testUsers[rrjsuser.id+""]=rrjsuser;
				console.log($scope.testUsers);
			}
			delete $scope.tmpUsers[guidArr[key]];
		},function(data){
			alert("删除出错");
		});
    };
	$scope.userOrderChange=function(orderId,user2GroupId){
		JFHttp.post({
			url:"http://192.168.1.101:8080/userR2group/set",
			data:{
				orderId:orderId,
				user2GroupId:user2GroupId
			}
		}).then(function(result){

		},function(result){
			alert("绑定订单出错"+result.status);
		});
	};
})
//useCtrl
.controller('useCtrl', function($scope,$state,SweetAlert,JFHttp) {
	localStorage.clear();
	$scope.navToEdit=function(item){
		var uid=(new Date()).valueOf();
		localStorage.setItem(uid+"",JSON.stringify(item));
		$state.go("app.objEdi",{id:uid})
	}
	JFHttp.post({
		url:"http://192.168.1.101:8080/dest/all"
	}).then(function(result){
		$scope.itmes=result.data;
	},function(result){
		alert("获取出错，请稍候再试"+result.status);
	});
	$scope.delDest=function(id){
		SweetAlert.swal({   
			title: "确定删除?",   
			text: "删除后不可恢复!",   
			type: "warning",   
			showCancelButton: true,   
			confirmButtonColor: "#DD6B55",   
			confirmButtonText: "心意已决，删除它!",   
			cancelButtonText: "不，我再看看",   
			closeOnConfirm: false,   
			closeOnCancel: false 
		}, function(isConfirm){  
			if (isConfirm) {     
				JFHttp.post({
					url:"http://192.168.1.101:8080/dest/del",
					data:{
						id:id
					}
				}).then(function(result){
					SweetAlert.swal("已删除!", "已经删除它了.", "success"); 
				},function(result){
					SweetAlert.swal("删除失败", "", "error");   
				})
				  
			} else {     
				SweetAlert.swal("已取消", "已经取消", "error");   
			} 
		});
	}  
})
//ObjAddCtrl 目的地添加
.controller('setCtrl', ['$scope', '$http','$state','JFHttp', function($scope, $http,$state,JFHttp) {
    $scope.reader = new FileReader();   //创建一个FileReader接口
    $scope.thumb = {};      //用于存放图片的base64
    $scope.thumb_default = {    //用于循环默认的‘加号’添加图片的框
        1111:{}
    };
	var sdsf=(new Date()).valueOf()+Math.random()*100;
	$scope.guid=sdsf;
	$scope.config={};
	JFHttp.post({
		url:"http://192.168.1.101:8080/config/get",
	}).then(function(result){
		$scope.config=result.data;
		if($scope.config.imgSrc){
			$scope.thumb[$scope.guid]={
				imgSrc:$scope.config.imgSrc,
			}
		}
		
	},function(result){

	});
	$scope.belongTo="";
    $scope.img_upload = function(files) {       //单次提交图片的函数
        $scope.guid = (new Date()).valueOf();   //通过时间戳创建一个随机数，作为键名使用
        $scope.reader.readAsDataURL(files[0]);  //FileReader的方法，把图片转成base64
        $scope.reader.onload = function(ev) {
            $scope.$apply(function(){
                $scope.thumb[$scope.guid] = {
                    imgSrc : ev.target.result,  //接收base64
                }
            });
        };
        
        var data = new FormData();      //以下为像后台提交图片数据
        data.append('file', files[0]);
        data.append('name',"1213");  
		//data.append('belongTo',$scope.belongTo);               
        $http({
            method: 'post',
            url: 'http://192.168.1.101:8080/file/saveImg',
            data:data,
              transformRequest: angular.identity ,
            headers: {'optional-header': 'header-value','Content-Type':undefined},
        }).success(function(data) {
			 $scope.thumb[$scope.guid].id=data.data.id;
			 $scope.belongTo=data.data.belongTo;
             console.log( $scope.thumb[$scope.guid]);
                    
        })
    };
	$scope.addADest=function(obj){
		obj.affixBelongTo=$scope.belongTo;
		JFHttp.post({
			url:"http://192.168.1.101:8080/config/set",
			data:obj,
		}).then(function(result){
		},function(result){
			alert("出错，请稍候再试。"+result.status);
		});
	}
}])
;